﻿// OperatorLibraryDialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "OperatorLibraryDialog.h"
#include "afxdialogex.h"


// COperatorLibraryDialog 对话框

IMPLEMENT_DYNAMIC(COperatorLibraryDialog, CDialogEx)

COperatorLibraryDialog::COperatorLibraryDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_OPERATOR_LIBRARY, pParent)
	, m_strModName(_T(""))
	, m_bShowAllOpers(FALSE)
{

}

COperatorLibraryDialog::~COperatorLibraryDialog()
{

}

void COperatorLibraryDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TREE_OPERATOR, m_tree);
	DDX_Control(pDX, IDC_LIST_OPERATOR, m_list);
	DDX_Control(pDX, IDC_COMBO_MODULE, m_cmbModule);
	DDX_CBString(pDX, IDC_COMBO_MODULE, m_strModName);
	DDX_Check(pDX, IDC_CHECK_OPER_SHOW_ALL, m_bShowAllOpers);
}


BEGIN_MESSAGE_MAP(COperatorLibraryDialog, CDialogEx)
	ON_CBN_SELCHANGE(IDC_COMBO_MODULE, &COperatorLibraryDialog::OnCbnSelchangeComboModule)
	ON_NOTIFY(NM_CLICK, IDC_LIST_OPERATOR, &COperatorLibraryDialog::OnNMClickListOperator)
	ON_NOTIFY(NM_CLICK, IDC_TREE_OPERATOR, &COperatorLibraryDialog::OnNMClickTreeOperator)
	ON_BN_CLICKED(IDC_CHECK_OPER_SHOW_ALL, &COperatorLibraryDialog::OnBnClickedCheckOperShowAll)
END_MESSAGE_MAP()


// COperatorLibraryDialog 消息处理程序


BOOL COperatorLibraryDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	m_tree.AttachBitmap();
	m_tree.Populate();

	m_list.Init2();

	PopulateModuleCombo();

	m_bShowAllOpers = TRUE;
	UpdateData(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void COperatorLibraryDialog::PopulateModuleCombo()
{
	ck::Module* pModule = NULL;
	ck::ModuleManager* pModManager = ck::ModuleManager::GetInstance();

	m_cmbModule.Clear();
	int nCount = pModManager->GetCount();
	for (int i = 0; i < nCount; i++)
	{
		pModule = pModManager->GetModule(i);
		int nItem = m_cmbModule.AddString(pModule->GetName());
		m_cmbModule.SetItemData(nItem, (DWORD)pModule->GetCode());
	}
	if (m_cmbModule.GetCount() > 0)
	{
		m_cmbModule.SetCurSel(0);
		z_uint32 nModeCode = (z_uint32)m_cmbModule.GetItemData(0);
		PopulateOperator(nModeCode);
	}
}


void COperatorLibraryDialog::OnCbnSelchangeComboModule()
{
	// TODO: 在此添加控件通知处理程序代码
	z_uint32 nModeCode = (z_uint32)m_cmbModule.GetItemData(m_cmbModule.GetCurSel());
	PopulateOperator(nModeCode);
}

void COperatorLibraryDialog::OnNMClickListOperator(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码

	DWORD dwPos = GetMessagePos();
	CPoint point(LOWORD(dwPos), HIWORD(dwPos));
	m_list.ScreenToClient(&point);

	UINT nFlag = 0;
	int nItem = m_list.HitTest(point, &nFlag);

	if (nItem >= 0)
	{
		if (nFlag == LVHT_ONITEMSTATEICON)
		{
			BOOL checked = !m_list.GetCheck(nItem);
			//选中COperatorListCtrl的CheckBox，实现挂载和卸载Operator			
			if (!m_tree.GetSelectedItem())
			{
				m_list.SetCheck(nItem, checked);
				AfxMessageBox("未选中检查项");
			}
			else
			{
				int nNodeType = m_tree.GetNodeType(m_tree.GetSelectedItem());
				if (nNodeType != OPER_TREE_ITEM_NODE)
				{
					m_list.SetCheck(nItem, checked);
					AfxMessageBox("未选中检查项");
				}
				else
				{
					//1)检查项Code
					z_uint32 nItemCode = (z_uint32)m_tree.GetItemData(m_tree.GetSelectedItem());
					//2)模块Code
					z_uint32 nModCode = (z_uint32)m_cmbModule.GetItemData(m_cmbModule.GetCurSel());
					//3)算子Code
					z_uint32 nOperCode = (z_uint32)m_list.GetItemData(nItem);
					
					if (checked)
					{
						ck::ElementRepository* pElementRepository = ck::ElementRepository::GetInstance();
						RESULTCODE rc = pElementRepository->MountOperator(nItemCode, nOperCode, nModCode);
						if (rc != RC_SUCCESS)
							AfxMessageBox("挂载检查算子失败");
						else
							AfxMessageBox("挂载检查算子成功");
					}
					else
					{
						ck::ElementRepository* pElementRepository = ck::ElementRepository::GetInstance();
						RESULTCODE rc = pElementRepository->MountOperator(nItemCode, nOperCode, nModCode);
						if (rc != RC_SUCCESS)
							AfxMessageBox("卸载检查算子失败");
						else
							AfxMessageBox("卸载检查算子成功");
					}
				}	
			}
		}
	}

	*pResult = 0;
}


void COperatorLibraryDialog::OnNMClickTreeOperator(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: 在此添加控件通知处理程序代码

	CPoint pt;
	if (!GetCursorPos(&pt))
	{
		return;
	}

	m_tree.ScreenToClient(&pt);
	UINT uFlag = 0;
	HTREEITEM hItem = m_tree.HitTest(pt, &uFlag);
	if ((NULL == hItem) || !(TVHT_ONITEM&uFlag))
	{
		return;
	}

	m_tree.Select(hItem, TVGN_CARET);
	int nNodeType = m_tree.GetNodeType(m_tree.GetSelectedItem());
	switch (nNodeType)
	{
	case OPER_TREE_ITEM_NODE:
	{
		PopulateOperaotrList();
	}
	break;
	default:
		break;
	}


	*pResult = 0;
}

void COperatorLibraryDialog::PopulateOperaotrList()
{
	//1)检查项Code
	int nn = m_cmbModule.GetCurSel();
	z_uint32 nCheckItemCode = (z_uint32)m_tree.GetItemData(m_tree.GetSelectedItem());
	z_uint32 nModIndex = m_cmbModule.GetCurSel();// 0;// m_cmbModule.FindString(m_cmbModule.GetWindowText());
	z_uint32 nModeCode = m_cmbModule.GetItemData(nModIndex);

	ck::Module* pModule = NULL;
	ck::ModuleManager* pModManager = ck::ModuleManager::GetInstance();
	pModule = pModManager->GetModuleByCode(nModeCode);
	if (pModule)
	{
		m_list.Populate(pModule, nCheckItemCode,m_bShowAllOpers);
	}
}


void COperatorLibraryDialog::PopulateOperator(z_uint32 nModeCode)
{
	ck::Module* pModule = NULL;
	ck::ModuleManager* pModManager = ck::ModuleManager::GetInstance();
	pModule = pModManager->GetModuleByCode(nModeCode);
	if (pModule)
	{
		m_list.Populate(pModule);
	}
	else
	{
		m_list.DeleteAllItems();
	}
}


void COperatorLibraryDialog::OnBnClickedCheckOperShowAll()
{
	// TODO: 在此添加控件通知处理程序代码
	m_bShowAllOpers = !m_bShowAllOpers;
	UpdateData(FALSE);

	PopulateOperaotrList();
}
